package com.qiankun.tang.ui.view;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.library.listener.OnAppListener;
import com.common.library.tools.Logger;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.ui.view.adapter.HorizontalListAdapter;

import java.util.List;

/**
 * @author txf
 * @Title 横向列表
 * @package com.qiankun.video.ui.view
 * @date 2017/6/12 0012
 */

public class HorizontalListWidget extends LinearLayout implements OnAppListener.OnInteractionListener, ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager mViewPager;
    private LinearLayout mIndicatorLayout;
    private HorizontalListAdapter mHorizontalListAdapter;
    LinearLayout.LayoutParams lp;
    List<ADBean> mDatas;
    private int IndicatorSelectedColor;
    private int IndicatorUnSelectedColor;
    private OnClickListaner l;

    private TextView mSubtitle;
    private TextView mTitle;

    public HorizontalListWidget(@NonNull Context context) {
        super(context);
        init();
    }

    public HorizontalListWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HorizontalListWidget(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_horizontal_list_widget, this);
        mViewPager = (ViewPager) findViewById(R.id.view_horizontal_list_widget_viewPager);
        mIndicatorLayout = (LinearLayout) findViewById(R.id.view_horizontal_list_widget_indicatorLayout);

        mTitle = (TextView) findViewById(R.id.view_horizontal_list_widget_title);
        mSubtitle = (TextView) findViewById(R.id.view_horizontal_list_widget_subtitle);

        findViewById(R.id.view_horizontal_list_widget_follow).setOnClickListener(this);

        setPageStyle();

        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(mHorizontalListAdapter = new HorizontalListAdapter(this));
        mViewPager.addOnPageChangeListener(this);

        IndicatorSelectedColor = R.drawable.round_444444;
        IndicatorUnSelectedColor = R.drawable.round_f1f1f1;
    }
    /**
     * 特殊需求 改变viewPager显示样式
     * */
    public void setPageStyle() {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mViewPager.getLayoutParams();
        lp.leftMargin = 50;
        lp.rightMargin = 50;
        mViewPager.setLayoutParams(lp);
        mViewPager.setPageMargin(15);
    }
    private void setTitleText(int pos) {
        mSubtitle.setText(mDatas.get(pos).getSubtitle());
        mTitle.setText(mDatas.get(pos).getTitle());
    }
    /**
     * @param pos      默认从pos位置开始
     */
    public void setData(List<ADBean> datas, int pos) {
        mDatas = datas;
        mHorizontalListAdapter.setData(mDatas, true);
        addIndicator(mDatas, pos);
        setTitleText(pos);

        int value = (Integer.MAX_VALUE / 2) % mDatas.size()-1;
        mViewPager.setCurrentItem(Integer.MAX_VALUE / 2 + value + pos,false);
    }

    private void addIndicator(List<ADBean> beans, int pos) {
        if (mIndicatorLayout.getChildCount() != 0) {
            mIndicatorLayout.removeAllViews();
        }
        for (int i = 0; i < beans.size(); i++) {
            View v = new View(getContext());
            if (i == pos)
                v.setBackgroundResource(IndicatorSelectedColor);
            else
                v.setBackgroundResource(IndicatorUnSelectedColor);
            if (lp == null) {
                lp = new LinearLayout.LayoutParams(dip2px(7), dip2px(7));
                lp.setMargins(0, 0, dip2px(5), 0);
            }
            mIndicatorLayout.addView(v, lp);
        }
    }
    private void setIndicator(int pos) {
        int count = mIndicatorLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            if (i == pos) {
                mIndicatorLayout.getChildAt(i).setBackgroundResource(IndicatorSelectedColor);
            } else {
                mIndicatorLayout.getChildAt(i).setBackgroundResource(IndicatorUnSelectedColor);
            }
        }
    }
    private int dip2px(float dipValue) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.view_horizontal_list_widget_follow:
                //TODO 点击关注
                Logger.i("点击关注");
                break;
        }
    }
    public interface OnClickListaner {
        void onItemClick(ADBean bean);
    }
    public void addOnClickListaner(OnClickListaner l) {
        this.l = l;
    }

    /**
     * pagerItem 点击监听
     */
    @Override
    public void onInteraction(int type, Object o) {
        if (l != null) {
            l.onItemClick((ADBean) o);
        }
    }
    /**
     * pager切换监听
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        position = position % mDatas.size();
    }
    @Override
    public void onPageSelected(int position) {
        position = position % mDatas.size();
        setIndicator(position);
        setTitleText(position);
    }
    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
